﻿using System;
using System.Web.UI;
using System.Web.Security;

namespace WebGui.User {
    /// <summary>
    /// this is the profile page here you can see the profil info.
    /// </summary>
    public partial class Profile : Page {

        MembershipUser bruger;
        /// <summary>
        /// Loads the page 
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            bruger = Membership.GetUser(User.Identity.Name);
            if(!IsPostBack) {
                TxtNavn.Text = bruger.Email;
                TxtAdr.Text = bruger.Comment;
            }
        }
        /// <summary>
        /// updates the profil with a new password
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void BtnUpdateUser_Click(object sender, EventArgs e) {
            ClearLabels();
            if(TxtNytPW.Text.ToString() == TxtNytPW2.Text.ToString()) {
                try {
                    if(bruger.ChangePassword(TxtGammeltPW.Text, TxtNytPW.Text)) {
                        LblPWStatus.Text = "Dit password er skiftet";
                    } else {
                        LblPWStatus.Text = "Dit password blev ikke skiftet, er dit gamle password korrekt?";
                    }
                } catch(Exception) {
                    LblPWStatus.Text = "Der skete en fejl, prøv igen";
                }
            } else {
                LblPWFail.Text = "De 2 passwords er ikke ens";
            }
        }

        protected void ClearLabels() {
            LblPWFail.Text = "";
            LblPWStatus.Text = "";
        }
        /// <summary>
        /// Update the profile with the new info
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void BtnProfileUpdate_Click(object sender, EventArgs e) {
            try {
                bruger.Email = TxtNavn.Text.ToString();
                bruger.Comment = TxtAdr.Text;
                Membership.UpdateUser(bruger);
            } catch(System.Configuration.Provider.ProviderException a) {
                LblPWFail.Text = a.Message;
            }
        }
    }
}